package com.shop.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.common.SetMeta;
import com.shop.common.page.CategoryPage;
import com.shop.exception.CategoryException;
import com.shop.mapper.CategoryMapper;
import com.shop.entity.Category;
import com.shop.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    public CategoryMapper categoryMapper;

    private List<Category> categoryList;

    /**
     * 商品分类数据列表(不分页)
     * @param type
     * @return
     */
    @Override
    public SetMeta selectCategory(Integer type) throws CategoryException{
        SetMeta setMeta = new SetMeta();
        categoryList = categoryMapper.selectList(null);

        if(type == null) type = 3;
        CategoryLevel level = CategoryLevel.getCategoryLevel(type - 1);

        List<Category> levelOne = getCategoryList(CategoryLevel.LEVEL_ONE.level);
        List<Category> levelTwo;
        List<Category> levelThree;
        if (level == null) throw new CategoryException("分类参数级别错误");
        switch (level) {
            case LEVEL_ONE:
                break;
            case LEVEL_TWO:
                levelTwo = getCategoryList(CategoryLevel.LEVEL_TWO.level);
                merge(levelOne,levelTwo);
                break;
            case LEVEL_THREE:
                levelTwo = getCategoryList(CategoryLevel.LEVEL_TWO.level);
                merge(levelOne, levelTwo);
                levelThree = getCategoryList(CategoryLevel.LEVEL_THREE.level);
                merge(levelTwo,levelThree);
                break;
            default:
                throw new CategoryException("分类参数级别错误");
        }
        setMeta.setSuccess(levelOne);
        return setMeta;
    }

    /**
     * 商品分类数据列表(分页)
     * @param type
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public SetMeta selectPageCategory(Integer type, Integer pagenum, Integer pagesize) throws CategoryException {
        SetMeta<CategoryPage> setMeta = new SetMeta();
        Page<Category> categoryPage = categoryMapper.selectPage(new Page<>(pagenum, pagesize), Wrappers.<Category>lambdaQuery().eq(Category::getCat_level, CategoryLevel.LEVEL_ONE.level));
        List<Category> level_one = categoryPage.getRecords();
        List<Category> level_two;
        List<Category> level_three;

        categoryList = categoryMapper.selectList(Wrappers.<Category>lambdaQuery().eq(Category::getCat_level, CategoryLevel.LEVEL_TWO.level).or().eq(Category::getCat_level, CategoryLevel.LEVEL_THREE.level));

        if (type == null) {
            type = 3;
        }
        CategoryLevel level = CategoryLevel.getCategoryLevel(type - 1);
        if (level == null) {
            throw new CategoryException("分类参数级别异常");
        }

        switch (level) {
            case LEVEL_ONE:
                break;
            case LEVEL_TWO:
                level_two = getCategoryList(CategoryLevel.LEVEL_TWO.level);
                merge(level_one, level_two);
                break;
            case LEVEL_THREE:
                level_two = getCategoryList(CategoryLevel.LEVEL_TWO.level);
                merge(level_one, level_two);
                level_three = getCategoryList(CategoryLevel.LEVEL_THREE.level);
                merge(level_two, level_three);
                break;
            default:
                throw new CategoryException("分类参数级别异常");
        }
        setMeta.setSuccess(new CategoryPage(categoryPage.getTotal(),pagesize,level_one));
        return setMeta;
    }

    private enum CategoryLevel {
        LEVEL_ONE(0, "一级分类"),
        LEVEL_TWO(1, "二级分类"),
        LEVEL_THREE(2, "三级分类");

        private Integer level;
        private String msg;

        CategoryLevel(Integer level, String msg) {
            this.level = level;
            this.msg = msg;
        }

        public static CategoryLevel getCategoryLevel(Integer type) {
            CategoryLevel[] values = CategoryLevel.values();
            for (CategoryLevel value : values) {
                if (value.level.equals(type)) {
                    return value;
                }
            }
            return null;
        }
    }

    private List<Category> getCategoryList(Integer type) {
        List<Category> categoryList = new ArrayList<>();
        for (Category category : this.categoryList) {
            if (category.getCat_level().equals(type)) {
                categoryList.add(category);
            }
        }
        return categoryList;
    }

    private void merge(List<Category> parents, List<Category> childs) {
        for (Category parent : parents) {
            List<Category> temp = new ArrayList<>();
            for (Category child : childs) {
                if (child.getCat_pid().equals(parent.getCat_id())) {
                    temp.add(child);
                }
            }
            parent.setChildren(temp);
        }
    }

    /**
     * 添加分类
     * @param category
     * @return
     */
    @Override
    public SetMeta<Category> addCategory(Category category) throws CategoryException {
        SetMeta<Category> setMeta = new SetMeta<>();
        if(category.getCat_name() == null || category.getCat_level() == null || category.getCat_pid() == null) throw new CategoryException("相关参数为空");

        Category select = categoryMapper.selectById(category.getCat_pid());
        if(select == null){ throw new CategoryException("父类级别错误");}


        int insert = categoryMapper.insert(category);
        if (insert == 1) {
            category.setCatDeleted(null);
            setMeta.setCreated(category, "创建成功");
            return setMeta;
        }

        setMeta.setError("创建失败");
        return setMeta;
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    @Override
    public SetMeta<Category> selectById(Integer id) {
        Category category = categoryMapper.selectById(id);
        SetMeta<Category> setMeta = new SetMeta<>();
        if (category != null) {
            category.setCatDeleted(null);
            setMeta.setSuccess(category);
            return setMeta;
        }
        setMeta.setError();
        return setMeta;
    }

    /**
     * 编辑提交分类
     * @param id
     * @param cat_name
     * @return
     */
    @Override
    public SetMeta<Category> updateCategory(Integer id, String cat_name) {
        SetMeta<Category> setMeta = new SetMeta();
        if (cat_name != null && !cat_name.equals("")) {
            Category category = categoryMapper.selectById(id);
            category.setCatName(cat_name);
            int update = categoryMapper.update(category, Wrappers.<Category>lambdaQuery().eq(Category::getCat_id, id));
            if (update == 1) {
                category.setCatDeleted(null);
                setMeta.setSuccess(category, "更新成功");
                return setMeta;
            }
        }
        setMeta.setError("更新失败");
        return setMeta;
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @Override
    public SetMeta<Category> delete(Integer id) {
        SetMeta<Category> setMeta = new SetMeta<Category>();
        int delete = categoryMapper.delete(Wrappers.<Category>lambdaQuery().eq(Category::getCat_id, id));
        if (delete == 1) {
            setMeta.setSuccess("删除成功");
            return setMeta;
        }
        setMeta.setError("删除失败");
        return setMeta;
    }

}
